package Classic150;

import Tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 二叉树的右视图 {

    List<Integer> res = new ArrayList<>();

    public List<Integer> rightSideView(TreeNode root) {
        /*
            递归
         */
        rightSideViewRecursion(root, 0);
        return res;

        /*
            迭代
         */
        /*if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            for (int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                if (i == 1) res.add(node.val);
            }
        }
        return res;*/
    }

    private void rightSideViewRecursion(TreeNode node, int deep) {
        if (node == null) return;
        if (deep == res.size()) res.add(node.val);
        rightSideViewRecursion(node.right, deep + 1);
        rightSideViewRecursion(node.left, deep + 1);
    }
}
